package com.itheima.web;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.pojo.pageBean;
import com.itheima.service.BrandService;
import com.itheima.service.BrandServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet {

    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Test2.class);    // 创建Spring容器
    BrandServiceImpl brandService1 = (BrandServiceImpl) applicationContext.getBean("brandService");
    public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        List<Brand> brands = brandService.selectAll();
//
//        String s = JSON.toJSONString(brands);
//
//        resp.setContentType("text/json;charset=utf-8");
//
//        resp.getWriter().write(s);


        List<Brand> brands = brandService1.selectAll();

        String s = JSON.toJSONString(brands);

        resp.setContentType("text/json;charset=utf-8");

        resp.getWriter().write(s);

    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String s = req.getReader().readLine();

        Brand brand = JSON.parseObject(s, Brand.class);

        brandService1.Add(brand);

        resp.getWriter().write("success");
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

          String s = req.getParameter("id");

          brandService1.deleteById(Integer.parseInt(s));

          resp.getWriter().write("success");
    }


    public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String s = req.getReader().readLine();

        int[] ints = JSON.parseObject(s, int[].class);

        brandService1.deleteByIds(ints);

        resp.getWriter().write("success");
    }


    public void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        pageBean<Brand> pageBean =  brandService1.selectByPage(
                Integer.parseInt(currentPage),
                Integer.parseInt(pageSize));

        String s = JSON.toJSONString(pageBean);
        System.out.println("s:   " +s);

        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(s);

    }

    public void selectByPageAndCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");

        String s1 = req.getReader().readLine();

        Brand brand = JSON.parseObject(s1, Brand.class);

        pageBean<Brand> pageBean = brandService1.selectByPageAndCondition(
                Integer.parseInt(currentPage),
                Integer.parseInt(pageSize),
                brand);

        String s = JSON.toJSONString(pageBean);

        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(s);

    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String s = req.getReader().readLine();

        Brand brand = JSON.parseObject(s, Brand.class);

        brandService1.updateById(brand);

        resp.getWriter().write("success");

    }  public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");

        String password = req.getParameter("password");


    }


}
